﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Timers;
using System.Threading;

namespace server
{

    class Program
    {


        static UdpClient server = new UdpClient(9059);
        static List<Client> list = new List<Client>();

        static void Main(string[] args)
        {
            var p = new Points();
            p.openforvote = true;
            while (true)
            {

                if (p.openforvote)
                {
                    var remote = new IPEndPoint(IPAddress.Any, 9059);

                    var data = server.Receive(ref remote);
                    string s = Encoding.ASCII.GetString(data);
                    Console.WriteLine("Data från: " + remote.ToString() + "   " + s);

                    if (s == "TEST")
                    {
                        string ReadIPandPort = remote.ToString();
                        string[] IPandPort = ReadIPandPort.Split(':');
                        Client c = new Client(IPandPort[0], Convert.ToInt16(IPandPort[1]));
                        server.Send(new byte[] { 1 }, 1, remote);
                        int connected  = 0;
                        for (int i = list.Count - 1; i >= 0; --i)
                        {
                            if (list[i] == c)
                                connected++;
                        }
                        if (connected == 0)
                        {
                            list.Add(c);
                        }
                    }
                }
            }
        }

    }
}
